package com.singing.manager;

import com.singing.dao.AdminDao;
import com.singing.model.Admin;
import com.singing.model.Command;

import java.util.List;
import java.util.Scanner;

public class AdminRegistManager extends BaseManager {

    // 输入管理员注册信息
    private static final int INPUT_ADMIN_INFO = 1;

    AdminDao adminDao = new AdminDao();

    public AdminRegistManager(String title, List<Command> commandList) {
        super(title, commandList);
    }

    @Override
    protected int processCommand(int command) {
        if (command == INPUT_ADMIN_INFO) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入管理员姓名");
            String name = scanner.next();
            System.out.println("输入管理员账号");
            String account = scanner.next();
            String password = "";
            while (true) {
                System.out.println("输入管理员密码");
                password = scanner.next();
                System.out.println("输入确认密码");
                String confirmPassword = scanner.next();
                if (!password.equals(confirmPassword)) {
                    System.err.println("密码和确认密码不相同,请重新输入");
                } else {
                    break;
                }
            }

            Admin admin = registerAdmin(new Admin(name, account, password));
            if (admin != null) {
                System.out.println("注册成功");
            } else {
                System.err.println("注册失败");
            }
        }
        return command;
    }


    private Admin registerAdmin(Admin admin) {
        int count = adminDao.insert(admin);
        if (count == 1) {
            return admin;
        } else {
            return null;
        }
    }
}
